#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    server = new WebServer("WebServer", QWebSocketServer::NonSecureMode, this);
    connect(server, SIGNAL(clientConnected()), this, SLOT(clientConnected()));
    connect(server, SIGNAL(clientDisconnected()), this, SLOT(clientDisconnected()));
    connect(server, SIGNAL(receiveData(QString)), this, SLOT(receiveData(QString)));

    ui->send_button->setEnabled(false);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    if(ui->ip_linEdit->text().isEmpty() || ui->port_lineEdit->text().isEmpty())
        return ;

    QString ip = ui->ip_linEdit->text();
    int port = ui->port_lineEdit->text().toInt();

    server->start(ip, port);

    ui->pushButton->setEnabled(false);
}

void MainWindow::clientConnected()
{
    ui->send_button->setEnabled(true);
}

void MainWindow::clientDisconnected()
{
    ui->send_button->setEnabled(false);
}


void MainWindow::receiveData(const QString &data)
{
    ui->recv_lineEdit->setText(data);
}




void MainWindow::on_send_button_clicked()
{
    QString data = ui->send_lineEdit->text();
    server->sendData(data);
}
